<template>
  <div class="permission-container">
    <div class="app-container">
      <head-tool>
        <template slot="after-box">
          <el-button
            type="primary"
            @click="showAdd('0', 1)"
            size="small"
            
            >添加权限</el-button
          >
        </template>
      </head-tool>
      <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        >
        <el-table-column prop="name" label="名称"> </el-table-column>
        <el-table-column
          align="center"
          prop="code"
          label="标识"
          style="text-align:center"
        >
        </el-table-column>
        <el-table-column align="center" prop="description" label="描述">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="{ row }">
            <el-link
              v-if="row.type == 1"
              type="primary"
              @click="showAdd(row.id, 2)"
              >添加</el-link
            >
            <el-link
              style="margin:0 20px"
              @click="doEdit(row.id)"
              type="primary"
              >编辑</el-link
            >
            <el-link type="primary" @click="doDel(row.id)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="title" :visible.sync="showDialog">
      <el-form :model="form" label-width="120px">
        <el-form-item label="权限名称">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="form.enVisible"
            active-value="1"
            inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-row type="flex" justify="center">
          <el-button type="primary" @click="save">确 定</el-button>
          <el-button>取 消</el-button>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPermissionList, //获取所有权限列表
  getPermissionDetail, //根据id获取权限详情
  addPermission, //新增权限
  updatePermission, //根据id修改权限
  removePermission //删除权限
} from "@/api/permission";

// 树形结构递归导入
import { transToTreeData } from "@/utils";
export default {
  name: "permission",
  data() {
    return {
      tableData: [],
      form: {},
      showDialog: false
    };
  },
  methods: {
    // 删除权限
    async doDel(id) {
      await removePermission(id);
      // 成功提示
      this.$message.success("删除成功");
      // 刷新列表
      this.getPermissionList();
    },
    // 编辑权限
    async doEdit(id) {
      let res = await getPermissionDetail(id);
      this.form = res.data;
      this.showDialog = true;
    },
    // 点击确定提交表单
    async save() {
      if (this.form.id) {
        // 做编辑
        await updatePermission(this.form);
      } else {
        // 调用接口发请求
        await addPermission(this.form);
      }
      // console.log(res);
      // 成功提示
      this.$message.success(this.title + "成功");
      // 刷新列表
      this.getPermissionList();
      // 隐藏对话框
      this.showDialog = false;
    },
    // 点击添加
    // pid  几级
    // type 有两个添加按钮 区分开来
    showAdd(pid, type) {
      // 应为form 里面没有pid  个和type  传值给他方便区分
      this.form.type = type;
      this.form.pid = pid;
      this.showDialog = true;
    },
    // 获取所有权限列表
    async getPermissionList() {
      let res = await getPermissionList();
      this.tableData = await transToTreeData(res.data, "0");
    }
  },
  created() {
    this.getPermissionList();
  },
  computed: {
    title() {
      return this.form.id ? "编辑权限" : "新增权限";
    }
  }
};
</script>

<style lang="scss" scoped>
.permission-container {
  ::v-deep .el-dialog__header {
    background-color: #09f;
  }
}
</style>
